using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//This lets us be pushed back by collisions
[RequireComponent(typeof(CharacterController))]

public class Character : Damagable {

    public List<GameObject> attacks;
    public enum Facing {Right, Left};
    public Facing facing;

    protected bool isAttacking; //Do we have an attack in play?

    protected void MoveCharacter(Vector3 deltaPos)
    {
        deltaPos.y = -1.0f*transform.position.y ;
        gameObject.GetComponent<CharacterController>().Move(deltaPos);

        //Compute facing and mirror sprites
        if (deltaPos.x < 0)
        {
            SetFacing(Facing.Left);
        }
        else if (deltaPos.x > 0)
        {
            SetFacing(Facing.Right);
        }
    }

    public void SetFacing(Facing newFacing)
    {
        if (facing != newFacing)
        {
            facing = newFacing;
            if (facing == Facing.Left)
            {
                Vector3 scale = transform.localScale;
                scale.x = -1 * Mathf.Abs(scale.x);
                transform.localScale = scale;
            }
            else
            {
                Vector3 scale = transform.localScale;
                scale.x = Mathf.Abs(scale.x);
                transform.localScale = scale;
            }
        }
    }

    protected void Attack(int attackID)
    {
        if (attackID >= 0 && attackID < attacks.Count)
        {
            GameObject attack = attacks[attackID];
            if (attack)
            {
                isAttacking = true;
                //Create attack, parent it to character, and clear transform
                GameObject attackInstance = Instantiate(attack) as GameObject;
                attackInstance.GetComponent<Attack>().attacker = gameObject;
                attackInstance.GetComponent<Attack>().PositionAttack();
            }
            else
            {
                Debug.Log("Attempting to execute null attack", this);
            }
        }
        else
        {
            Debug.Log("Invalid attack ID", this);
        }
    }

    public virtual void OnAttackComplete()
    {
        isAttacking = false;
    }
}
